package com.tidc.seaservermodel.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/10/13 20:25
 * @description：
 * @modified By：
 * @version:
 */
@Configuration
public class RedisConfig {
    public static String REGISTER_CODE = "registerCode:";
    public static String ADD_SECONDARY_CODE = "addSecondaryCode:";
    public static String CHECK_IP = "checkIp:";
    public static String PUT_SECONDARY_CODE = "putSecondaryCode:";
    public static String REPETITION_RABBIT = "repetitionRabbit:";
//    @Bean
//    public RedisCacheManager redisCacheManager(RedisConnectionFactory factory) {
//        //解决序列化
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        // Json序列化器
//        Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        jsonRedisSerializer.setObjectMapper(objectMapper);
//        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jsonRedisSerializer));
//        Map<String, RedisCacheConfiguration> map = new HashMap<>();
////        key是jdk value是string
//        map.put("school_news@", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(3))
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))
//        .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer)));
//        map.put("school_htmlToJson@", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(7)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jsonRedisSerializer)));
//        return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(factory)
//                .withInitialCacheConfigurations(map).cacheDefaults(cacheConfiguration)
//                .build();
//    }
}
